package org.example.reservas_mesas;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.example.library.ErrorLogin;
import org.example.library.Invoker;
import org.example.library.ServidorPHP;
import org.example.library.ValidarCampos;
import org.example.library.ValidarUsuarios;

public class principal extends Activity {
    
    String user;
    String pass;
    Button blogin;
    TextView registrar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        blogin = (Button) findViewById(R.id.Blogin);
        registrar =(Button) findViewById(R.id.link_to_register);

        blogin.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                user = ((EditText) findViewById(R.id.edusuario)).getText().toString();
                pass = ((EditText) findViewById(R.id.edpassword)).getText().toString();
                ValidarCampos validar = new ValidarCampos();
                int check = validar.checklogindata(user,pass);
  
                if( check == 0){
                    Invoker invoker = new Invoker();
                    ServidorPHP servidor = new ServidorPHP();
                    servidor.SetMailComensal(user);
                    servidor.SetPassComensal(pass);
                    ValidarUsuarios VU = new ValidarUsuarios(servidor);
                    
                    invoker.SetPeticion(VU);
                    invoker.Ejecuta();
                    
                    if(servidor.GetResultado() != null && servidor.GetResultado().get(0).get("estado").equals(1+"")){
                        Intent i = new Intent(principal.this,usuario.class);
                        i.putExtra("usuario",user);
                        startActivity(i);
                    }
                    else{
                        ErrorLogin errores = new ErrorLogin(3);
                        errores.printerror(getApplicationContext());
                    }
                }
                else{
                    ErrorLogin errores = new ErrorLogin(check);
                    errores.printerror(getApplicationContext());
                }
            }
        });

        registrar.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                    Intent i=new Intent(principal.this, altanueva.class);
                    startActivity(i);   		
                }      	
        });
    }
}
